package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/depts")
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    @GetMapping
    public Result findAll(){
        log.info("查询所有部门信息");
       List<Dept> deptList = deptService.findAll();
       return Result.success(deptList);
    }

    @DeleteMapping
    public Result deleteById(Integer id){
        log.info("根据id删除部门信息{}",id);
        deptService.deleteById(id);
        return Result.success();
    }

    @PostMapping
    public Result addDept(@RequestBody Dept dept){
        log.info("新增部门信息{}",dept);
        deptService.addDept(dept);
        return Result.success();
    }

    @GetMapping("/{id}")//@PathVariable注解表示获取路径中的占位符,根据id查询部门信息
    public Result findById(@PathVariable Integer id){
        log.info("根据id查询部门信息{}",id);
        Dept dept = deptService.findById(id);
        return Result.success(dept);
    }

    @PutMapping
    public Result updateDept(@RequestBody Dept dept){
        log.info("修改部门信息{}",dept);
        deptService.updateDept(dept);
        return Result.success();
    }

}
